package com.ymatou.iapi.settlement.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.settlement.parameter.OrderSettlementReq;
import com.ymatou.iapi.settlement.parameter.SettleSellerIncomeAndChargeCommissionBean;
import com.ymatou.iapi.settlement.service.GenerateInvoicesOnOrderReceivedCall;
import com.ymatou.iapi.settlement.service.SettleSellerIncomeAndChargeCommissionCall;
import com.ymatou.iapi.trading.operate.parameter.ActivityVo;
import com.ymatou.iapi.trading.operate.parameter.OrderGroupVo;
import com.ymatou.iapi.trading.operate.parameter.OrderItemVo;
import com.ymatou.iapi.trading.operate.parameter.OrderVo;
import com.ymatou.iapi.trading.operate.parameter.SellerCouponVo;
import com.ymatou.iapi.trading.operate.parameter.YmatouCouponVo;
import com.ymatou.iapi.trading.operate.parameter.enums.ActivityPromotionTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.AppTerminalSourceEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.OrderSourceEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.OrderTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.PriceTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.ProductRefundChannelEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.SalesTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.SellerCouponApplianceTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.TerminalSourceEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.YmatouCouponApplianceTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.trading.operate.service.PlaceOrderCall;
import com.ymt.base.YmatouEasyCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.CouponCallService;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.ProductCallService;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/**
 * 结算收入和扣佣接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SettleSellerIncomeAndChargeCommission {
	private static SettleSellerIncomeAndChargeCommissionBean settlesellerincomeandchargecommissionBean;
	private static SettleSellerIncomeAndChargeCommissionCall call;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("结算收入和扣佣接口");
	}

	@Before
	public void caseUp() {
		settlesellerincomeandchargecommissionBean = new SettleSellerIncomeAndChargeCommissionBean();
		call = new SettleSellerIncomeAndChargeCommissionCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * 一个订单下面多个商品
	 */
	private Map placeOrder() {
		Map map = new HashMap();
		try {
			PlaceOrderReq placeorderBean = new PlaceOrderReq();
			PlaceOrderCall placeorderCall = new PlaceOrderCall();
			// 注册新用户
			Map newUser = new HashMap();
			newUser = new UserCallService().registerByMobile();
			int userId = (Integer) newUser.get("userid");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			// 创建产品
			Logger.comment("前置数据准备");
			HashMap<String, Object> productInfo = ProductCallService
					.createProduct(sellerId, 3);
			Logger.comment("前置数据准备结束");
			String productId = (String) productInfo.get("productId");
			Logger.debug("productId:" + productId);
			String productName = (String) productInfo.get("productName");
			String pic = (String) productInfo.get("pic");
			BigDecimal price = (BigDecimal) (productInfo.get("price"));
			String catalogId = (String) productInfo.get("catalogId");
			YmatouCouponVo ymtCoupon = new YmatouCouponVo();
			// 平台优惠券
			BigDecimal couponValue = new BigDecimal(13);
			String couponCode = new OrderCallService().receivePlatformCoupon(
					userId, couponValue);
			ymtCoupon.setApplianceType(YmatouCouponApplianceTypeEnum.ALL);
			ymtCoupon.setCouponCode(couponCode);
			ymtCoupon.setCouponValue(couponValue);
			placeorderBean.setAppId("AutoTest");
			placeorderBean.setYmatouCoupon(ymtCoupon);
			placeorderBean.setUserId(userId);
			placeorderBean.setRequestId(TestDataManager.getRandomUUID()
					.toString());
			placeorderBean.setTerminalSource(TerminalSourceEnum.IOS);
			placeorderBean.setAppTerminalSource(AppTerminalSourceEnum.Phone);
			placeorderBean.setSourceIp("127.0.0.1");
			placeorderBean.setAppId("ios");
			placeorderBean.setDeviceId("7777777777888888888888999");
			OrderGroupVo groupVo1 = new OrderGroupVo();
			placeorderBean.setOrderSource(OrderSourceEnum.PC);
			// 商家优惠券
			BigDecimal sellerCouponValue = new BigDecimal(15);
			String sellerCouponCode = new CouponCallService()
					.receiveCouponInfo(sellerId, userId, couponValue);
			SellerCouponVo sellerCoupon = new SellerCouponVo();
			sellerCoupon.setApplianceType(SellerCouponApplianceTypeEnum.ALL);
			sellerCoupon.setCouponCode(sellerCouponCode);
			sellerCoupon.setCouponValue(sellerCouponValue);
			groupVo1.setSellerCoupon(sellerCoupon);
			groupVo1.setSellerId(sellerId);
			List<OrderVo> orders = new ArrayList<OrderVo>();
			OrderVo ordervo1 = new OrderVo();
			ordervo1.setAutoCancelOrderHours(new BigDecimal(1));
			ordervo1.setOrderType(OrderTypeEnum.Spot);
			ordervo1.setPaidFull(true);
			ordervo1.setAddress("上海市闸北区灵石路636号");
			ordervo1.setLeaveWord("ymt autotest");
			ordervo1.setPhone("13100000001");
			ordervo1.setPostCode("200001");
			ordervo1.setTelephone("021-51002100");
			ordervo1.setReceiveName("李四");
			List<OrderItemVo> items = new ArrayList<OrderItemVo>();
			OrderItemVo orderitem1 = new OrderItemVo();
			orderitem1.setCatalogId(TestDataManager.getRandomUUID().toString());
			orderitem1.setProductNum(3);
			orderitem1.setProductName(productName);
			orderitem1.setPriceType(PriceTypeEnum.VipPrice);
			orderitem1.setFirstCategoryId(1149);
			orderitem1.setProductId(TestDataManager.getRandomUUID().toString());
			price = new BigDecimal(210);
			orderitem1.setEarnestPrice(price);
			orderitem1.setProductPrice(price);
			orderitem1.setOriginalPrice(price);
			orderitem1.setSalesType(SalesTypeEnum.AlreadyInStock);
			orderitem1.setLogisticsType(LogisticsTypeEnum.SailProtect);
			orderitem1
					.setProductRefundChannel(ProductRefundChannelEnum.Official);
			orderitem1.setBrandId(5);
			orderitem1.setPackageNo("11111111");
			orderitem1.setFirstCategoryId(1111111);
			orderitem1.setSecondCategoryId(222222);
			orderitem1.setThirdCategoryId(333333333);
			orderitem1
					.setProductDetailInfo("e5a1eccf-6a14-458f-b907-35dc9a3d2713#适用人群:b13e9eb1-98d6-4d39-8c35-225758e443d3#男");
			orderitem1.setProductRecordNo("1234567");
			orderitem1
					.setProductRefundChannel(ProductRefundChannelEnum.Official);
			orderitem1
					.setProperty("06f25e0c-e431-44ae-8a07-4d0bc824e3bd#尺寸:951a814e-2ded-4241-b9a0-91b2cd9a44fd#6,d2f663ea-3214-4344-abce-a8eaffcc43ce#颜色:f3ed3ad7-a084-450b-b698-256542963306#红色");
			ActivityVo activity = new ActivityVo();
			activity.setActivityId(46870);
			activity.setActivityBeginDate(YMTDateUtil.getBeforeOrNextDay(-10,
					"yyyy-MM-dd'T'HH:mm:ss"));
			activity.setActivityEndDate(YMTDateUtil.getBeforeOrNextDay(10,
					"yyyy-MM-dd'T'HH:mm:ss"));
			activity.setActivityTemplateId(12345678);
			activity.setCommissionRate(new BigDecimal(1.1));
			activity.setCost(true);
			activity.setFeeFree(false);
			activity.setPremium(new BigDecimal(1.5));
			activity.setPromotionPrice(new BigDecimal(15));
			activity.setPromotionType(ActivityPromotionTypeEnum.DiscountRate);
			activity.setUseCoupon(true);
			orderitem1.setActivity(activity);
			ordervo1.setOrderItems(items);
			ordervo1.setFreight(new BigDecimal("21"));
			// 第2件商品
			OrderItemVo orderitem2 = new OrderItemVo();
			orderitem2.setCatalogId("edbcc2c9-1ba9-4b04-ab88-940f63271823");
			orderitem2.setProductNum(1);
			orderitem2.setProductName(productName);
			orderitem2.setPriceType(PriceTypeEnum.VipPrice);
			orderitem2.setProductId(TestDataManager.getRandomUUID().toString());
			orderitem2.setFirstCategoryId(1149);
			orderitem2.setSecondCategoryId(2222);
			orderitem2.setThirdCategoryId(3333);
			price = new BigDecimal(70);
			orderitem2.setEarnestPrice(price);
			orderitem2.setProductPrice(price);
			orderitem2.setOriginalPrice(price);
			orderitem2.setSalesType(SalesTypeEnum.AlreadyInStock);
			orderitem2.setLogisticsType(LogisticsTypeEnum.SailProtect);
			orderitem2
					.setProductRefundChannel(ProductRefundChannelEnum.Official);
			orderitem2.setBrandId(5);
			orderitem2.setPackageNo("11111111");
			orderitem2
					.setProductDetailInfo("e5a1eccf-6a14-458f-b907-35dc9a3d2713#适用人群:b13e9eb1-98d6-4d39-8c35-225758e443d3#男");
			orderitem2.setProductRecordNo("1234567");
			orderitem2
					.setProductRefundChannel(ProductRefundChannelEnum.Official);
			orderitem2
					.setProperty("06f25e0c-e431-44ae-8a07-4d0bc824e3bd#尺寸:951a814e-2ded-4241-b9a0-91b2cd9a44fd#6,d2f663ea-3214-4344-abce-a8eaffcc43ce#颜色:f3ed3ad7-a084-450b-b698-256542963306#红色");
			orderitem2.setActivity(activity);
			items.add(orderitem1);
			items.add(orderitem2);
			ordervo1.setOrderItems(items);
			ordervo1.setFreight(new BigDecimal("21"));
			orders.add(ordervo1);
			groupVo1.setOrders(orders);
			placeorderBean.setOrderGroups(Arrays.asList(groupVo1));
			placeorderCall.setData(false, placeorderBean);
			placeorderCall.callService();
			JSONArray orderIds = placeorderCall.getOrderIds();
			int tradingId = placeorderCall.getTradingId();
			map.put("orderIds", orderIds);
			map.put("tradingId", tradingId);
		} catch (Exception e) {
			Logger.fail(e);
		}
		return map;
	}

	/**
	 * 一个订单下面多个商品, 新用户老客单 买家在买手 店铺支付完成的第
	 * 
	 * 一单所在日期之后的其他订单（同一买家在该买手店铺支付完成的订单）为该买手的老客订单
	 */
	private Map placeOrder4() {
		Map map = new HashMap();
		try {
			PlaceOrderReq placeorderBean = new PlaceOrderReq();
			PlaceOrderCall placeorderCall = new PlaceOrderCall();
			// 注册新用户
			Map newUser = new HashMap();
			newUser = new UserCallService().registerByMobile();

			int userId = (Integer) newUser.get("userid");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));

			// 审核在90天内的新买手
			// sellerId = 20224977;
			Map firstOrder = new HashMap();
			firstOrder = new OrderCallService().createM2cOrder(newUser);
			int orderId = (Integer) firstOrder.get("orderId");
			int tradingId = (Integer) firstOrder.get("tradingId");
			BigDecimal totalPrice = (BigDecimal) firstOrder.get("totalPrice");// 支付金额
			// 支付
			new OrderCallService().orderPayRequest(userId, totalPrice,
					tradingId);
			// 更新订单支付时间
			new DefaultWapper().updateOrderPaidTimeByOrderId(orderId);
			// 创建产品
			Logger.comment("前置数据准备");
			HashMap<String, Object> productInfo = ProductCallService
					.createProduct(sellerId, 3);
			Logger.comment("前置数据准备结束");
			String productId = (String) productInfo.get("productId");
			Logger.debug("productId:" + productId);
			String productName = (String) productInfo.get("productName");
			String pic = (String) productInfo.get("pic");
			BigDecimal price = (BigDecimal) (productInfo.get("price"));
			String catalogId = (String) productInfo.get("catalogId");
			YmatouCouponVo ymtCoupon = new YmatouCouponVo();
			// 平台优惠券
			BigDecimal couponValue = new BigDecimal(13);
			String couponCode = new OrderCallService().receivePlatformCoupon(
					userId, couponValue);
			ymtCoupon.setApplianceType(YmatouCouponApplianceTypeEnum.ALL);
			ymtCoupon.setCouponCode(couponCode);
			ymtCoupon.setCouponValue(couponValue);
			placeorderBean.setAppId("AutoTest");
			placeorderBean.setYmatouCoupon(ymtCoupon);
			placeorderBean.setUserId(userId);
			placeorderBean.setRequestId(TestDataManager.getRandomUUID()
					.toString());
			placeorderBean.setTerminalSource(TerminalSourceEnum.IOS);
			placeorderBean.setAppTerminalSource(AppTerminalSourceEnum.Phone);
			placeorderBean.setSourceIp("127.0.0.1");
			placeorderBean.setAppId("ios");
			placeorderBean.setDeviceId("7777777777888888888888999");
			OrderGroupVo groupVo1 = new OrderGroupVo();
			placeorderBean.setOrderSource(OrderSourceEnum.PC);
			// 商家优惠券
			BigDecimal sellerCouponValue = new BigDecimal(15);
			String sellerCouponCode = new CouponCallService()
					.receiveCouponInfo(sellerId, userId, couponValue);
			SellerCouponVo sellerCoupon = new SellerCouponVo();
			sellerCoupon.setApplianceType(SellerCouponApplianceTypeEnum.ALL);
			sellerCoupon.setCouponCode(sellerCouponCode);
			sellerCoupon.setCouponValue(sellerCouponValue);
			groupVo1.setSellerCoupon(sellerCoupon);
			groupVo1.setSellerId(sellerId);
			List<OrderVo> orders = new ArrayList<OrderVo>();
			OrderVo ordervo1 = new OrderVo();
			ordervo1.setAutoCancelOrderHours(new BigDecimal(1));
			ordervo1.setOrderType(OrderTypeEnum.Spot);
			ordervo1.setPaidFull(true);
			ordervo1.setAddress("上海市闸北区灵石路636号");
			ordervo1.setLeaveWord("ymt autotest");
			ordervo1.setPhone("13100000001");
			ordervo1.setPostCode("200001");
			ordervo1.setTelephone("021-51002100");
			ordervo1.setReceiveName("李四");
			List<OrderItemVo> items = new ArrayList<OrderItemVo>();
			OrderItemVo orderitem1 = new OrderItemVo();
			orderitem1.setCatalogId(TestDataManager.getRandomUUID().toString());
			orderitem1.setProductNum(3);
			orderitem1.setProductName(productName);
			orderitem1.setPriceType(PriceTypeEnum.VipPrice);
			orderitem1.setFirstCategoryId(1149);
			orderitem1.setSecondCategoryId(2222);
			orderitem1.setThirdCategoryId(3333);
			orderitem1.setProductId(TestDataManager.getRandomUUID().toString());

			price = new BigDecimal(21);
			orderitem1.setEarnestPrice(price);
			orderitem1.setProductPrice(price);
			orderitem1.setOriginalPrice(price);
			orderitem1.setSalesType(SalesTypeEnum.AlreadyInStock);
			orderitem1.setLogisticsType(LogisticsTypeEnum.SailProtect);
			orderitem1
					.setProductRefundChannel(ProductRefundChannelEnum.Official);
			orderitem1.setBrandId(5);
			orderitem1.setPackageNo("11111111");
			orderitem1
					.setProductDetailInfo("e5a1eccf-6a14-458f-b907-35dc9a3d2713#适用人群:b13e9eb1-98d6-4d39-8c35-225758e443d3#男");
			orderitem1.setProductRecordNo("1234567");
			orderitem1
					.setProductRefundChannel(ProductRefundChannelEnum.Official);
			orderitem1
					.setProperty("06f25e0c-e431-44ae-8a07-4d0bc824e3bd#尺寸:951a814e-2ded-4241-b9a0-91b2cd9a44fd#6,d2f663ea-3214-4344-abce-a8eaffcc43ce#颜色:f3ed3ad7-a084-450b-b698-256542963306#红色");
			ActivityVo activity = new ActivityVo();
			activity.setActivityId(46870);
			activity.setActivityBeginDate(YMTDateUtil.getBeforeOrNextDay(-10,
					"yyyy-MM-dd'T'HH:mm:ss"));
			activity.setActivityEndDate(YMTDateUtil.getBeforeOrNextDay(10,
					"yyyy-MM-dd'T'HH:mm:ss"));
			activity.setActivityTemplateId(12345678);
			activity.setCommissionRate(new BigDecimal(1.1));
			activity.setCost(true);
			activity.setFeeFree(false);
			activity.setPremium(new BigDecimal(1.5));
			activity.setPromotionPrice(new BigDecimal(15));
			activity.setPromotionType(ActivityPromotionTypeEnum.DiscountRate);
			activity.setUseCoupon(true);
			orderitem1.setActivity(activity);
			ordervo1.setOrderItems(items);
			ordervo1.setFreight(new BigDecimal("21"));
			// 第2件商品
			OrderItemVo orderitem2 = new OrderItemVo();
			orderitem2.setCatalogId("edbcc2c9-1ba9-4b04-ab88-940f63271823");
			orderitem2.setProductNum(1);
			orderitem2.setProductName(productName);
			orderitem2.setPriceType(PriceTypeEnum.VipPrice);
			orderitem2.setProductId(TestDataManager.getRandomUUID().toString());
			orderitem2.setFirstCategoryId(1149);
			orderitem2.setSecondCategoryId(2222);
			orderitem2.setThirdCategoryId(3333);
			price = new BigDecimal(30);
			orderitem2.setEarnestPrice(price);
			orderitem2.setProductPrice(price);
			orderitem2.setOriginalPrice(price);
			orderitem2.setSalesType(SalesTypeEnum.AlreadyInStock);
			orderitem2.setLogisticsType(LogisticsTypeEnum.SailProtect);
			orderitem2
					.setProductRefundChannel(ProductRefundChannelEnum.Official);
			orderitem2.setBrandId(5);
			orderitem2.setPackageNo("11111111");
			orderitem2
					.setProductDetailInfo("e5a1eccf-6a14-458f-b907-35dc9a3d2713#适用人群:b13e9eb1-98d6-4d39-8c35-225758e443d3#男");
			orderitem2.setProductRecordNo("1234567");
			orderitem2
					.setProductRefundChannel(ProductRefundChannelEnum.Official);
			orderitem2
					.setProperty("06f25e0c-e431-44ae-8a07-4d0bc824e3bd#尺寸:951a814e-2ded-4241-b9a0-91b2cd9a44fd#6,d2f663ea-3214-4344-abce-a8eaffcc43ce#颜色:f3ed3ad7-a084-450b-b698-256542963306#红色");
			orderitem2.setActivity(activity);
			items.add(orderitem1);
			items.add(orderitem2);
			ordervo1.setOrderItems(items);
			ordervo1.setFreight(new BigDecimal("21"));
			orders.add(ordervo1);
			groupVo1.setOrders(orders);
			placeorderBean.setOrderGroups(Arrays.asList(groupVo1));
			placeorderCall.setData(false, placeorderBean);
			placeorderCall.callService();
			JSONArray orderIds = placeorderCall.getOrderIds();
			int tradingId2 = placeorderCall.getTradingId();
			map.put("userId", userId);
			map.put("sellerId", sellerId);
			map.put("orderIds", orderIds);
			map.put("tradingId", tradingId2);
		} catch (Exception e) {
			Logger.fail(e);
		}
		return map;
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SettleSellerIncomeAndChargeCommission_001() {
		Logger.start(true, "结算收入和扣佣成功");
		try {
			OrderSettlementReq generateinvoicesonorderreceivedBean = new OrderSettlementReq();
			GenerateInvoicesOnOrderReceivedCall generateinvoicesonorderreceivedCall = new GenerateInvoicesOnOrderReceivedCall();
			// 创建订单
			Logger.comment("前置数据准备");
			Map map = placeOrder();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			JSONArray orderIds = (JSONArray) map.get("orderIds");
			int tradingId = (Integer) map.get("tradingId");
			int orderId = orderIds.getInt(0);
			OrderCallService orderCallService = new OrderCallService();
			// 应付款全金额
			BigDecimal totalPrice = orderCallService
					.getNeedPrePayAmountOfCash(orderId);
			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);

			YmatouEasyCall payPayMqSenderCall = new YmatouEasyCall("", "POST",
					"JSON");
			String payPayMqSenderString = "{\"orderId\":" + orderId + "}";
			payPayMqSenderCall
					.setUrl("http://operate.trading.iapi.ymatou.com/api/trading/onOrderPaid");
			payPayMqSenderCall.setData(payPayMqSenderString);
			Logger.comment("支付发送消息");
			payPayMqSenderCall.callService();
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);
			// 商家发货
			orderCallService.dispatched(orderId, sellerId);
			// 确认收货
			orderCallService.confirmReceive(userId, orderId);
			generateinvoicesonorderreceivedBean.setOrderId(orderId);
			generateinvoicesonorderreceivedCall.setData(false,
					generateinvoicesonorderreceivedBean);
			Logger.comment("生成订单结算,扣拥结算单");
			generateinvoicesonorderreceivedCall.callService();
			Logger.comment("前置数据准备结束");
			// 结算前账户金额
			DefaultWapper defaultWapper = new DefaultWapper();
			HashMap<String, Integer> m1 = new HashMap<String, Integer>();
			m1.put("UserId", sellerId);
			BigDecimal price1 = (BigDecimal) defaultWapper
					.selectAccountInfo(m1).get(0).get("fAvailAmount");
			settlesellerincomeandchargecommissionBean.setOrderId(orderId);
			call.setData(settlesellerincomeandchargecommissionBean);
			call.callService();
			Logger.verifyEquals("ok", call.getReturnData(), "验证返回报文");
			BigDecimal price2 = (BigDecimal) defaultWapper
					.selectAccountInfo(m1).get(0).get("fAvailAmount");
			// 查询交易退款结算表
			List<Map> settlementInvoiceList = defaultWapper
					.selectYmtSettlementInvoice(orderId);

			BigDecimal settlemt1 = settlementInvoiceList.stream()
					.filter(m -> (Integer) m.get("BusinessType") == 1)
					.map(m -> (BigDecimal) m.get("TotalAmount"))
					.reduce(BigDecimal.ZERO, BigDecimal::add);
			BigDecimal settlemt2 = settlementInvoiceList.stream()
					.filter(m -> (Integer) m.get("BusinessType") == 4)
					.map(m -> (BigDecimal) m.get("TotalAmount"))
					.reduce(BigDecimal.ZERO, BigDecimal::add);

			Logger.verifyEquals(settlemt1.subtract(settlemt2),
					price2.subtract(price1), "验证商家账务余额变化");
			Logger.verifyEquals(
					1,
					(Integer) settlementInvoiceList.get(0).get(
							"SettlementStatus"), "验证SettlementStatus");
			Logger.verifyEquals("确认收货订单结算",
					(String) settlementInvoiceList.get(0).get("Remark"),
					"验证Remark");
			Logger.verifyEquals(
					1,
					(Integer) settlementInvoiceList.get(1).get(
							"SettlementStatus"), "验证SettlementStatus");
			Logger.verifyEquals("收取交易服务费结算", (String) settlementInvoiceList
					.get(1).get("Remark"), "验证Remark");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SettleSellerIncomeAndChargeCommission_002() {
		Logger.start(true, "重复结算");
		try {
			OrderSettlementReq generateinvoicesonorderreceivedBean = new OrderSettlementReq();
			GenerateInvoicesOnOrderReceivedCall generateinvoicesonorderreceivedCall = new GenerateInvoicesOnOrderReceivedCall();
			// 创建订单
			Logger.comment("前置数据准备");
			Map map = placeOrder();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			JSONArray orderIds = (JSONArray) map.get("orderIds");
			int tradingId = (Integer) map.get("tradingId");
			int orderId = orderIds.getInt(0);
			OrderCallService orderCallService = new OrderCallService();
			// 应付款全金额
			BigDecimal totalPrice = orderCallService
					.getNeedPrePayAmountOfCash(orderId);
			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);

			YmatouEasyCall payPayMqSenderCall = new YmatouEasyCall("", "POST",
					"JSON");
			String payPayMqSenderString = "{\"orderId\":" + orderId + "}";
			payPayMqSenderCall
					.setUrl("http://operate.trading.iapi.ymatou.com/api/trading/onOrderPaid");
			payPayMqSenderCall.setData(payPayMqSenderString);
			Logger.comment("支付发送消息");
			payPayMqSenderCall.callService();
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);
			// 商家发货
			orderCallService.dispatched(orderId, sellerId);
			// 确认收货
			orderCallService.confirmReceive(userId, orderId);
			generateinvoicesonorderreceivedBean.setOrderId(orderId);
			generateinvoicesonorderreceivedCall.setData(false,
					generateinvoicesonorderreceivedBean);
			Logger.comment("生成订单结算,扣拥结算单");
			generateinvoicesonorderreceivedCall.callService();
			Logger.comment("前置数据准备结束");
			// 结算前账户金额
			DefaultWapper defaultWapper = new DefaultWapper();
			HashMap<String, Integer> m1 = new HashMap<String, Integer>();
			m1.put("UserId", sellerId);
			BigDecimal price1 = (BigDecimal) defaultWapper
					.selectAccountInfo(m1).get(0).get("fAvailAmount");
			settlesellerincomeandchargecommissionBean.setOrderId(orderId);
			call.setData(settlesellerincomeandchargecommissionBean);
			call.callService();
			Logger.comment("重复结算");
			call.callService();

			Logger.verifyEquals("ok", call.getReturnData(), "验证返回报文");
			BigDecimal price2 = (BigDecimal) defaultWapper
					.selectAccountInfo(m1).get(0).get("fAvailAmount");
			// 查询交易退款结算表
			List<Map> settlementInvoiceList = defaultWapper
					.selectYmtSettlementInvoice(orderId);

			BigDecimal settlemt1 = settlementInvoiceList.stream()
					.filter(m -> (Integer) m.get("BusinessType") == 1)
					.map(m -> (BigDecimal) m.get("TotalAmount"))
					.reduce(BigDecimal.ZERO, BigDecimal::add);
			BigDecimal settlemt2 = settlementInvoiceList.stream()
					.filter(m -> (Integer) m.get("BusinessType") == 4)
					.map(m -> (BigDecimal) m.get("TotalAmount"))
					.reduce(BigDecimal.ZERO, BigDecimal::add);

			Logger.verifyEquals(settlemt1.subtract(settlemt2),
					price2.subtract(price1), "验证商家账务余额变化");
			Logger.verifyEquals(
					1,
					(Integer) settlementInvoiceList.get(0).get(
							"SettlementStatus"), "验证SettlementStatus");
			Logger.verifyEquals("确认收货订单结算",
					(String) settlementInvoiceList.get(0).get("Remark"),
					"验证Remark");
			Logger.verifyEquals(
					1,
					(Integer) settlementInvoiceList.get(1).get(
							"SettlementStatus"), "验证SettlementStatus");
			Logger.verifyEquals("收取交易服务费结算", (String) settlementInvoiceList
					.get(1).get("Remark"), "验证Remark");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SettleSellerIncomeAndChargeCommission_003() {
		Logger.start(false, "订单号为null");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			// settlesellerincomeandchargecommissionBean.setOrderId(orderId);
			call.setData(settlesellerincomeandchargecommissionBean);
			call.callService();

			Logger.verifyEquals("ok", call.getReturnData(), "验证返回报文");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SettleSellerIncomeAndChargeCommission_004() {
		Logger.start(true, "结算收入,扣佣金，返佣金，校验结算表，卖家资金流水表");
		try {
			OrderSettlementReq generateinvoicesonorderreceivedBean = new OrderSettlementReq();
			GenerateInvoicesOnOrderReceivedCall generateinvoicesonorderreceivedCall = new GenerateInvoicesOnOrderReceivedCall();
			// 创建订单
			Logger.comment("前置数据准备");
			Map map = placeOrder4();
			int userId = (Integer) map.get("userId");
			int sellerId = (Integer) map.get("sellerId");
			JSONArray orderIds = (JSONArray) map.get("orderIds");
			int tradingId = (Integer) map.get("tradingId");
			int orderId = orderIds.getInt(0);
			OrderCallService orderCallService = new OrderCallService();
			// 应付款全金额
			BigDecimal totalPrice = orderCallService
					.getNeedPrePayAmountOfCash(orderId);
			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);

			YmatouEasyCall payPayMqSenderCall = new YmatouEasyCall("", "POST",
					"JSON");
			String payPayMqSenderString = "{\"orderId\":" + orderId + "}";
			payPayMqSenderCall
					.setUrl("http://operate.trading.iapi.ymatou.com/api/trading/onOrderPaid");
			payPayMqSenderCall.setData(payPayMqSenderString);
			Logger.comment("支付发送消息");
			payPayMqSenderCall.callService();
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);
			// 商家发货
			orderCallService.dispatched(orderId, sellerId);
			// 确认收货
			orderCallService.confirmReceive(userId, orderId);
			generateinvoicesonorderreceivedBean.setOrderId(orderId);
			generateinvoicesonorderreceivedCall.setData(false,
					generateinvoicesonorderreceivedBean);
			Logger.comment("生成订单结算,扣拥结算单");
			generateinvoicesonorderreceivedCall.callService();
			Logger.comment("前置数据准备结束");
			// 结算前账户金额
			DefaultWapper defaultWapper = new DefaultWapper();
			HashMap<String, Integer> m1 = new HashMap<String, Integer>();
			m1.put("UserId", sellerId);
			BigDecimal price1 = (BigDecimal) defaultWapper
					.selectAccountInfo(m1).get(0).get("fAvailAmount");
			settlesellerincomeandchargecommissionBean.setOrderId(orderId);
			call.setData(settlesellerincomeandchargecommissionBean);
			call.callService();
			Logger.verifyEquals("ok", call.getReturnData(), "验证返回报文");
			BigDecimal price2 = (BigDecimal) defaultWapper
					.selectAccountInfo(m1).get(0).get("fAvailAmount");
			// 查询交易退款结算表
			List<Map> settlementInvoiceList = defaultWapper
					.selectYmtSettlementInvoice(orderId);
			// 订单结算
			BigDecimal settlemt1 = settlementInvoiceList.stream()
					.filter(m -> (Integer) m.get("BusinessType") == 1)
					.map(m -> (BigDecimal) m.get("TotalAmount"))
					.reduce(BigDecimal.ZERO, BigDecimal::add);
			// 订单扣佣
			BigDecimal settlemt2 = settlementInvoiceList.stream()
					.filter(m -> (Integer) m.get("BusinessType") == 4)
					.map(m -> (BigDecimal) m.get("TotalAmount"))
					.reduce(BigDecimal.ZERO, BigDecimal::add);
			// 订单返佣
			BigDecimal settlemt3 = settlementInvoiceList.stream()
					.filter(m -> (Integer) m.get("BusinessType") == 6)
					.map(m -> (BigDecimal) m.get("TotalAmount"))
					.reduce(BigDecimal.ZERO, BigDecimal::add);

			Logger.verifyEquals(settlemt1.add(settlemt3).subtract(settlemt2),
					price2.subtract(price1), "验证商家账务余额变化");
			Logger.verifyEquals(
					1,
					(Integer) settlementInvoiceList.get(0).get(
							"SettlementStatus"), "验证SettlementStatus");
			Logger.verifyEquals("确认收货订单结算",
					(String) settlementInvoiceList.get(0).get("Remark"),
					"验证Remark");
			Logger.verifyEquals(
					1,
					(Integer) settlementInvoiceList.get(1).get(
							"SettlementStatus"), "验证SettlementStatus");
			Logger.verifyEquals("收取交易服务费结算", (String) settlementInvoiceList
					.get(1).get("Remark"), "验证Remark");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}
